package p.lodz.pl.poid.zad2.controller;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import p.lodz.pl.poid.zad2.fourier.FourierProcessor;

/**
 * FXML Controller class
 *
 * @author janiu
 */
public class FiltersPanelController implements Initializable {
    
    @FXML
    private ToggleGroup filterTypeGroup;
    
    @FXML
    private RadioButton lowPassFilter;
    
    @FXML
    private TextField decimation;
    
    @FXML
    private RadioButton bandPassFilter;
    
    @FXML
    private Slider minBandValue;
    
    @FXML
    private Slider maxBandValue;
    
    @FXML
    private Slider power;
    
    @FXML
    private Button perform;

    private ImageView resultImage;
    private Image image;
    private BufferedImage bufferedImage;

    @Override
    public void initialize(URL url, ResourceBundle rb) {        
    }    

    @FXML
    private void performFiltering(ActionEvent event) {
        FiltersData data = prepareData();
        FourierProcessor process = new FourierProcessor();
        BufferedImage bufferedImage = process.filtersTransform(this.bufferedImage, data);
        Image image = Image.impl_fromExternalImage(bufferedImage);
        resultImage.setImage(image);
    }
    
    @FXML
    private void doLowPassFilter(MouseEvent event) {
        power.setDisable(false);
        decimation.setDisable(false);
        maxBandValue.setDisable(true);
        minBandValue.setDisable(true);
    }
    
    @FXML
    private void doBandPassFilter(MouseEvent event) {
        power.setDisable(false);
        decimation.setDisable(true);
        maxBandValue.setDisable(false);
        minBandValue.setDisable(false);
    }

    private FiltersData prepareData() {
        FiltersData data = new FiltersData();
        boolean isBandPassFilter = this.bandPassFilter.isSelected();
        boolean isLowPassFilter = this.lowPassFilter.isSelected();
        Double minBandValue = this.minBandValue.getValue();
        Double maxBandValue = this.maxBandValue.getValue();
        Double power = this.power.getValue();
        String decimationText = decimation.getText();
        decimationText = decimationText.isEmpty() ? "0" : decimationText;
        Double decimation = Double.parseDouble(decimationText);
        data.setBandPassFilterMarked(isBandPassFilter);
        data.setLowPassFilterMarked(isLowPassFilter);
        data.setMinimumBandValue(minBandValue);
        data.setMaximumBandValue(maxBandValue);
        data.setPower(power.intValue());
        data.setMaximumFrequency(decimation);
        return data;
    }
        
    public void setResultImage(ImageView resultImage) {
        this.resultImage = resultImage;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

    
}
